package com.zl.elasticsearchdemo.controller;

import cn.hutool.json.JSONUtil;
import com.zl.elasticsearchdemo.ESRequestUtils;
import com.zl.elasticsearchdemo.Employee;
import com.zl.elasticsearchdemo.MultiSearchEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLiang
 * @Date: 2021/08/05/14:21
 * @Description:
 */
@RestController
public class TestController {

    private final ESRequestUtils requestUtils;

    public TestController(ESRequestUtils requestUtils) {
        this.requestUtils = requestUtils;
    }

    @GetMapping("/{index}/{type}/{id}")
    public String jsonString(@PathVariable(value = "index") String index, @PathVariable(value = "type") String type, @PathVariable(value = "id") String id) {
        String jsonStr = requestUtils.getRequest(index, type, id);

        return jsonStr;
    }

    @GetMapping("/exists/{index}/{type}/{id}")
    public Boolean exists(@PathVariable(value = "index") String index, @PathVariable(value = "type") String type, @PathVariable(value = "id") String id) {
        Boolean exists = requestUtils.exists(index, type, id);

        return exists;
    }

    /**
     * 删除搜索节点
     * megacorp/employee/3
     *
     * @param index
     * @param type
     * @param id
     */
    @DeleteMapping("/{index}/{type}/{id}")
    public void delete(@PathVariable(value = "index") String index, @PathVariable(value = "type") String type, @PathVariable(value = "id") String id) {
        requestUtils.delete(index, type, id);
    }

    @PutMapping("/{index}/{type}/{id}")
    public void update(@PathVariable(value = "index") String index, @PathVariable(value = "type") String type, @PathVariable(value = "id") String id) {
        requestUtils.update(index, type, id);
    }

    @PostMapping(value = "/{index}/{type}/{id}")
    public void addIndex(@PathVariable(value = "index") String index, @PathVariable(value = "type") String type, @PathVariable(value = "id") String id, @RequestBody Employee employee) {
        System.out.println(JSONUtil.toJsonStr(employee));
        requestUtils.addIndex(index, type, id, JSONUtil.toJsonStr(employee));
    }

    /**
     * 根据索引查询
     * @param index
     * @return
     */
    @GetMapping("/search/{index}")
    public List<Employee> search(@PathVariable String index) {
        List<String> jsonString = requestUtils.searchRequest(index);
        System.out.println(jsonString);
        List<Employee> employees=  new ArrayList<>();
        for (String s: jsonString
             ) {
            Employee employee=JSONUtil.toBean(s,Employee.class);
            employees.add(employee);
        }
        return employees;
    }

    /**
     * 根据索引滚动查询
     * @param index
     * @return
     */
    @GetMapping("/searchScroll/{index}")
    public List<Employee> searchScroll(@PathVariable String index) {
        List<String> jsonString=requestUtils.searchScroll(index);
        System.out.println(jsonString);
        List<Employee> employees=  new ArrayList<>();
        for (String s: jsonString
        ) {
            Employee employee=JSONUtil.toBean(s,Employee.class);
            employees.add(employee);
        }
        return employees;
    }

    @GetMapping(value="/multiSearch")
    public void  multiSearch(){
        List<MultiSearchEntity> multiSearchEntities=new ArrayList<>();
        MultiSearchEntity multiSearchEntity1=new MultiSearchEntity();
        multiSearchEntity1.setIndex("megacorp");
       multiSearchEntity1.setText("I");
       multiSearchEntity1.setFieldNames(new String[]{"about"});
        multiSearchEntities.add(multiSearchEntity1);

        MultiSearchEntity multiSearchEntity2=new MultiSearchEntity();
        multiSearchEntity2.setIndex("megacorp");
        multiSearchEntity2.setText("32");
        multiSearchEntity2.setFieldNames(new String[]{"age"});
        multiSearchEntities.add(multiSearchEntity2);


        requestUtils.multiSearch(multiSearchEntities);
    }
}
